package com.shushan.setting;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.shushan.base.NavigationLoseActivity;
import com.shushan.login.VerificationCodeActivity;
import com.shushan.shushanbao.R;

/**
 * @ClassName: GradeSelectActivity
 * @Desciption: 年级选择
 * @author: yichaohua
 * @date: 2018-08-09
 */
public class GradeSettingActivity extends NavigationLoseActivity implements View.OnClickListener {

    private static final String TAG = "GradeSettingActivity";

    private RelativeLayout gradeLayout1,gradeLayout2,gradeLayout3,gradeLayout4,
            gradeLayout5,gradeLayout6,gradeLayout7,gradeLayout8,gradeLayout9,gradeLayout10,
            gradeLayout11,gradeLayout12;
    private TextView gradeText1,gradeText2,gradeText3,gradeText4,gradeText5,gradeText6,
            gradeText7,gradeText8,gradeText9,gradeText10,gradeText11,gradeText12;
    private TextView semesterText1,semesterText2,semesterText3,semesterText4,semesterText5,semesterText6,
            semesterText7,semesterText8,semesterText9,semesterText10,semesterText11,semesterText12;
    private Button confirmButton;

    private int gradeId = 1;
    private String semester = "UP";
    private int grade;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.grade_select_activity);

        grade = getIntent().getIntExtra("grade",1);

        setTitle("年级选择");

        initView();

        setGrade(grade);
    }

    private void initView() {
        confirmButton = findViewById(R.id.grade_set_confirm_btn);
        confirmButton.setOnClickListener(this);
        gradeLayout1 = findViewById(R.id.grade_set_grade1_layout);
        gradeLayout1.setOnClickListener(this);
        gradeLayout2 = findViewById(R.id.grade_set_grade2_layout);
        gradeLayout2.setOnClickListener(this);
        gradeLayout3 = findViewById(R.id.grade_set_grade3_layout);
        gradeLayout3.setOnClickListener(this);
        gradeLayout4 = findViewById(R.id.grade_set_grade4_layout);
        gradeLayout4.setOnClickListener(this);
        gradeLayout5 = findViewById(R.id.grade_set_grade5_layout);
        gradeLayout5.setOnClickListener(this);
        gradeLayout6 = findViewById(R.id.grade_set_grade6_layout);
        gradeLayout6.setOnClickListener(this);
        gradeLayout7 = findViewById(R.id.grade_set_grade7_layout);
        gradeLayout7.setOnClickListener(this);
        gradeLayout8 = findViewById(R.id.grade_set_grade8_layout);
        gradeLayout8.setOnClickListener(this);
        gradeLayout9 = findViewById(R.id.grade_set_grade9_layout);
        gradeLayout9.setOnClickListener(this);
        gradeLayout10 = findViewById(R.id.grade_set_grade10_layout);
        gradeLayout10.setOnClickListener(this);
        gradeLayout11 = findViewById(R.id.grade_set_grade11_layout);
        gradeLayout11.setOnClickListener(this);
        gradeLayout12 = findViewById(R.id.grade_set_grade12_layout);
        gradeLayout12.setOnClickListener(this);

        gradeText1 = findViewById(R.id.grade_set_grade1_tv);
        gradeText2 = findViewById(R.id.grade_set_grade2_tv);
        gradeText3 = findViewById(R.id.grade_set_grade3_tv);
        gradeText4 = findViewById(R.id.grade_set_grade4_tv);
        gradeText5 = findViewById(R.id.grade_set_grade5_tv);
        gradeText6 = findViewById(R.id.grade_set_grade6_tv);
        gradeText7 = findViewById(R.id.grade_set_grade7_tv);
        gradeText8 = findViewById(R.id.grade_set_grade8_tv);
        gradeText9 = findViewById(R.id.grade_set_grade9_tv);
        gradeText10 = findViewById(R.id.grade_set_grade10_tv);
        gradeText11 = findViewById(R.id.grade_set_grade11_tv);
        gradeText12 = findViewById(R.id.grade_set_grade12_tv);

        semesterText1 = findViewById(R.id.grade_set_semester1_tv);
        semesterText2 = findViewById(R.id.grade_set_semester2_tv);
        semesterText3 = findViewById(R.id.grade_set_semester3_tv);
        semesterText4 = findViewById(R.id.grade_set_semester4_tv);
        semesterText5 = findViewById(R.id.grade_set_semester5_tv);
        semesterText6 = findViewById(R.id.grade_set_semester6_tv);
        semesterText7 = findViewById(R.id.grade_set_semester7_tv);
        semesterText8 = findViewById(R.id.grade_set_semester8_tv);
        semesterText9 = findViewById(R.id.grade_set_semester9_tv);
        semesterText10 = findViewById(R.id.grade_set_semester10_tv);
        semesterText11 = findViewById(R.id.grade_set_semester11_tv);
        semesterText12 = findViewById(R.id.grade_set_semester12_tv);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.grade_set_grade1_layout:
                setGrade(1);
                break;
            case R.id.grade_set_grade2_layout:
                setGrade(2);
                break;
            case R.id.grade_set_grade3_layout:
                setGrade(3);
                break;
            case R.id.grade_set_grade4_layout:
                setGrade(4);
                break;
            case R.id.grade_set_grade5_layout:
                setGrade(5);
                break;
            case R.id.grade_set_grade6_layout:
                setGrade(6);
                break;
            case R.id.grade_set_grade7_layout:
                setGrade(7);
                break;
            case R.id.grade_set_grade8_layout:
                setGrade(8);
                break;
            case R.id.grade_set_grade9_layout:
                setGrade(9);
                break;
            case R.id.grade_set_grade10_layout:
                setGrade(10);
                break;
            case R.id.grade_set_grade11_layout:
                setGrade(11);
                break;
            case R.id.grade_set_grade12_layout:
                setGrade(12);
                break;
            case R.id.grade_set_confirm_btn:
                Intent intent = new Intent(this,BookSelectActivity.class);
                intent.putExtra("isSetGrade",true);
                intent.putExtra("gradeId",gradeId);
                intent.putExtra("semester",semester);
                startActivityForResultAllow(intent,400);
                break;
            default:
                break;
        }
    }

    private void setGrade(int grade) {
        gradeId = (grade%2==0)?(grade/2):((grade/2)+1);
        semester = (grade%2==0)?"DOWN":"UP";
        Log.i(TAG,"gradeId=" + gradeId + " semester=" + semester);

        gradeLayout1.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout2.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout3.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout4.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout5.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout6.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout7.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout8.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout9.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout10.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout11.setBackgroundResource(R.drawable.gray_ring_solid_shape);
        gradeLayout12.setBackgroundResource(R.drawable.gray_ring_solid_shape);

        gradeText1.setTextColor(Color.parseColor("#474747"));
        gradeText2.setTextColor(Color.parseColor("#474747"));
        gradeText3.setTextColor(Color.parseColor("#474747"));
        gradeText4.setTextColor(Color.parseColor("#474747"));
        gradeText5.setTextColor(Color.parseColor("#474747"));
        gradeText6.setTextColor(Color.parseColor("#474747"));
        gradeText7.setTextColor(Color.parseColor("#474747"));
        gradeText8.setTextColor(Color.parseColor("#474747"));
        gradeText9.setTextColor(Color.parseColor("#474747"));
        gradeText10.setTextColor(Color.parseColor("#474747"));
        gradeText11.setTextColor(Color.parseColor("#474747"));
        gradeText12.setTextColor(Color.parseColor("#474747"));

        semesterText1.setTextColor(Color.parseColor("#474747"));
        semesterText2.setTextColor(Color.parseColor("#474747"));
        semesterText3.setTextColor(Color.parseColor("#474747"));
        semesterText4.setTextColor(Color.parseColor("#474747"));
        semesterText5.setTextColor(Color.parseColor("#474747"));
        semesterText6.setTextColor(Color.parseColor("#474747"));
        semesterText7.setTextColor(Color.parseColor("#474747"));
        semesterText8.setTextColor(Color.parseColor("#474747"));
        semesterText9.setTextColor(Color.parseColor("#474747"));
        semesterText10.setTextColor(Color.parseColor("#474747"));
        semesterText11.setTextColor(Color.parseColor("#474747"));
        semesterText12.setTextColor(Color.parseColor("#474747"));

        switch (grade){
            case 1:
                gradeLayout1.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText1.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText1.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 2:
                gradeLayout2.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText2.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText2.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 3:
                gradeLayout3.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText3.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText3.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 4:
                gradeLayout4.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText4.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText4.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 5:
                gradeLayout5.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText5.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText5.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 6:
                gradeLayout6.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText6.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText6.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 7:
                gradeLayout7.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText7.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText7.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 8:
                gradeLayout8.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText8.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText8.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 9:
                gradeLayout9.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText9.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText9.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 10:
                gradeLayout10.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText10.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText10.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 11:
                gradeLayout11.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText11.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText11.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            case 12:
                gradeLayout12.setBackgroundResource(R.drawable.blue_ring_solid_shape);
                gradeText12.setTextColor(getResources().getColor(R.color.btn_blue));
                semesterText12.setTextColor(getResources().getColor(R.color.btn_blue));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        onBackAction(resultCode);
    }
}
